﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zyg.SSO.Core.Business;
using Zyg.Utility.DBUtility;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class SqlProvider : DALProvider
    {
        private static readonly String SqlInsertLog =
            "INSERT INTO [Log] ([DateCreated],[Message],[Action],[AccountID],[ModuleID]) VALUES (@DateCreated,@Message,@Action,@AccountId,@ModuleId)";
        private static readonly String SqlSelectAll =
            "SELECT [ID],[DateCreated],[Message],[Action],[AccountID],[ModuleID] FROM [Log] ORDER BY [ID] DESC";

        #region Log

        public override Log SelectLog(int id)
        {
            return null;
        }

        public override void DeleteLog(Log log)
        {
            ;
        }

        public override void UpdateLog(Log log)
        {
            ;
        }

        public override void InsertLog(Log log)
        {
            SqlServerUtility db = new SqlServerUtility();
            db.AddParameter("@DateCreated", SqlDbType.DateTime, log.DateCreated);
            db.AddParameter("@Message", SqlDbType.VarChar, log.Message);
            db.AddParameter("@Action", SqlDbType.VarChar, log.Action);
            db.AddParameter("@AccountId", SqlDbType.UniqueIdentifier, log.AccountId);
            db.AddParameter("@ModuleId", SqlDbType.UniqueIdentifier, log.ModuleId);
            db.ExecuteSql(SqlInsertLog);

        }

        public override IList<Business.Log> FillLogs()
        {
            IList<Log> results = new List<Log>();

            SqlServerUtility db = new SqlServerUtility();

            SqlDataReader reader = db.ExecuteSqlReader(SqlSelectAll);

            if (reader != null)
            {
                while (reader.Read())
                {
                    Log log = new Log();

                    if (!reader.IsDBNull(0)) log.Id = reader.GetInt32(0);
                    if (!reader.IsDBNull(1)) log.DateCreated = reader.GetDateTime(1);
                    if (!reader.IsDBNull(2)) log.Message = reader.GetString(2);
                    if (!reader.IsDBNull(3)) log.Action = reader.GetString(3);
                    if (!reader.IsDBNull(4)) log.AccountId = reader.GetGuid(4);
                    if (!reader.IsDBNull(5)) log.ModuleId = reader.GetGuid(5);

                    log.MarkOld();

                    results.Add(log);
                }

                reader.Close();
            }

            return results;
        }

        #endregion
    }
}
